package com.educationagent.util;

import com.educationagent.config.webSocket.WebSocketUsers;
import com.educationagent.service.MemoryAssistant;
import jakarta.websocket.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;


@Component
public class AIChatUtil {

    @Autowired
    private MemoryAssistant memoryAssistant;


    public void sendAI(String message, Session session) {

        Flux<String> chat = memoryAssistant.chat(session.getId().hashCode(), message);

        chat.subscribe(
                result -> {
                    WebSocketUsers.sendMessageToUserByText(session, result);

                },
                error -> {
                    // 处理错误
                    WebSocketUsers.sendMessageToUserByText(session, "Error: " + error.getMessage());
                }
        );
    }
}
